%% Exercise 1 - series 2
%% Visualization of Hadamard in the experimental space
%
% Table of Contents
% Exercise 1 - series 2
% Visualization of Hadamard in the experimental space
%     Design 1 - weighting object one by one
%     Design 2 - weighting object two by two
%     Design 3 - weighting object thee by three
%
%% Design 1 - weighting object one by one
%
% The graphic below represents the design corresponding to "measuring each object one by one". The left pan is coded '-1'  and the right pan is coded '1'. So when an object is not involved in the run, its codes value in the matrix of experiment is 0.
%
%
%
% The corresponding matrix of experiments has 4 rows and 3 columns:
%

X=[0 0 0;-1 0 0; 0 -1 0 ;0 0 -1];
disp(array2table(X))

%
% The corresponding model matrix has 4 rows and 4 columns:

% compute the model matrix
M=x2fx(X,"linear");

% display the model matrix
disp(array2table(M,'VariableNames',{'I' 'X1'  'X2'  'X3'}))

%
% The corresponding dispersion matrix has 4 rows and 4 columns:

% compute the dispersion matrix
D=inv(M'*M);

% display the dispersion matrix
disp(array2table(D,'VariableNames',{'I' 'X1'  'X2'  'X3'},'RowNames',{'I' 'X1'  'X2'  'X3'}))

%
% Use of the function dessin_3D to visualize the design :
% Each run (row in the matrix of experiment) corresponds to a point in the experimental space. The "visited space" is restricted to a  \frac{1}{16} of the experimental space. The variance of this design was 2 \sigma^2, if \sigma^2 represents the experimental variance due to the uncertainties linked to the scale.

% Parameter for the drawing
azimut=25;
elevation=15;
title='Design 1';
%
% call the function dessin_3D
dessin_3D(X,azimut,elevation,title)

%
%%
%%
%% Design 2 - weighting object two by two
%
% The graphic below represents the design corresponding to "measuring each object two by two". The left pan is coded '-1'  and the right pan is coded '1'. So when an object is not involved in the run, its codes value in the matrix of experiment is 0. For the first three runs, only two object are involved in the measurement. One on each pan. For he last run, all the objects have been paced on the right pan.
%
%
%
% The corresponding matrix of experiments is:

% coding the martrix of experiments
X=[1 -1 0;-1 0 1; 0 1 -1 ;-1 -1 -1];

% displaying the matrix of experiments
% the transformastion in table just for the display lets Matlab to indicate
% names for each culumn
disp(array2table(X))
% compute the model matrix
M=x2fx(X,"linear");

% display the model matrix
disp(array2table(M,'VariableNames',{'I' 'X1'  'X2'  'X3'}))
% compute the dispersion matrix
D=inv(M'*M);

% display the dispersion matrix
disp(array2table(D,'VariableNames',{'I' 'X1'  'X2'  'X3'},'RowNames',{'I' 'X1'  'X2'  'X3'}))

%
% Use of the function dessin_3D to visualize the design :
% Each run (row in the matrix of experiment) corresponds to a point in the experimental space. The "visited space" is restricted approximately to a  \frac{1}{3} of the experimental space. The variance of this design is in first approximation 0.37 \sigma^2, if \sigma^2 represents the experimental variance due to the uncertainties linked to the scale.

% Paramètres pour le dessin
azimut=25;
elevation=15;
titre='Plan 2';
%
% Invoque la fonction
dessin_3D(X,azimut,elevation,titre)

%
%% Design 3 - weighting object thee by three
%
%
%
% The corresponding matrix of experiments is:

X=[-1 1 1;1 -1 1;1 1 -1 ;-1 -1 -1];
disp(array2table(X))
% compute the model matrix
M=x2fx(X,"linear");

% display the model matrix
disp(array2table(M,'VariableNames',{'I' 'X1'  'X2'  'X3'}))
% compute the dispersion matrix
D=inv(M'*M);

% display the dispersion matrix
disp(array2table(D,'VariableNames',{'I' 'X1'  'X2'  'X3'},'RowNames',{'I' 'X1'  'X2'  'X3'}))

%
% Use of the function dessin_3D to visualize the design :
% Each run (row in the matrix of experiment) corresponds to a point in the experimental space. The "visited space" is restricted approximately to a  \frac{1}{2} of the experimental space. The variance of this design is in first approximation 0.25 \sigma^2, if \sigma^2 represents the experimental variance due to the uncertainties linked to the scale.

% Paramètres pour le dessin
azimut=25;
elevation=15;
titre='Plan 3';
%
% Invoque la fonction
dessin_3D(X,azimut,elevation,titre)

%%
%

function dessin_3D(X,az,el,titre)
% each line corresponds to a point in the experimental space

figure

plot3(X(:,1),X(:,2),X(:,3),'ok','MarkerSize',12,'MarkerFaceColor','r')
axis([-1 1 -1 1 -1 1])

view(az,el) % azimut and elevation of the point of view
title(titre,'FontName','Times New Roman','FontSize',20,'FontWeight','bold')
xlabel('Factor 1','FontName','Times New Roman','FontSize',14,'FontWeight','bold')
ylabel('Factor 2','FontName','Times New Roman','FontSize',14,'FontWeight','bold')
zlabel('Factor 3','FontName','Times New Roman','FontSize',14,'FontWeight','bold')
hold on
%
% * Draw a frame of reference xyz
quiver3(0,0,0,1,0,0,'LineWidth',3)
text(.9,.1,.1,'e_1','FontName','Times New Roman','FontSize',14,'FontWeight','bold')
quiver3(0,0,0,0,1,0,'LineWidth',3)
text(.1,.9,.1,'e_2','FontName','Times New Roman','FontSize',14,'FontWeight','bold')
quiver3(0,0,0,0,0,1,'LineWidth',3)
text(.1,.1,.9,'e_3','FontName','Times New Roman','FontSize',14,'FontWeight','bold')
%
% * draw a polygone based on the experimental points
S.Vertices = X;
S.Faces = convhull(X); 

% several possible functions
% bundary(), alphaShape()
S.FaceVertexCData = [0;1;2;3];
S.FaceColor = 'flat';
S.EdgeColor = 'black';
S.FaceAlpha= .5;
patch(S)
box on
grid on
hold off

end